package com.ambulance.project.biz.controller.treatter;


import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.framework.redis.RedisCache;
import com.ambulance.framework.security.service.SysLoginService;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.dto.treat.TreatUserInfo;
import com.ambulance.project.biz.enums.Source;
import com.ambulance.project.biz.service.SysCacheService;
import com.ambulance.project.biz.service.WebCustomerService;
import com.ambulance.project.biz.util.ALiSms;
import com.ambulance.project.manager.domain.BizHospital;
import com.ambulance.project.manager.service.IBizHospitalService;
import com.ambulance.utils.CommonUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 人员信息 前端控制器
 * </p>
 *
 * @author zr
 * @since 2020-04-21
 */
@Api(tags = "会诊子系统终端-登陆")
@RestController
@RequestMapping(value = "/treat/terminal/v1/user", method = RequestMethod.POST)
public class TreatTerUserController {


    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private WebCustomerService webCustomerService;
    @Autowired
    private SysCacheService sysCacheService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IBizHospitalService iBizHospitalService;

    /**
     * web会诊-发送验证码
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "发送验证码", notes = "发送验证码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/send/code")
    public Object preLogin(@RequestParam(value = "phone") String phone
    ) {


        sysCacheService.sendCode(phone);
        return AjaxResult.success();
    }

    /**
     * web会诊-登陆
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "登陆", notes = "登陆", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "type", value = "登陆方式,1手机验证码，2账户密码", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/login")
    public Object login(@RequestParam(value = "code") String code,
                        @RequestParam(value = "phone") String phone,
                        @RequestParam(value = "type", defaultValue = "1", required = false) String type
    ) {
        String token = null;
        if (type.equals("1")) {
            token = sysLoginService.loginNoCaptcha(phone, code, Source.TER);

        } else {
            token = sysLoginService.loginNoCaptcha(phone, code, Source.WYD);
        }
        return AjaxResult.success(token);

    }

    @ApiOperation(value = "登陆用户信息", notes = "登陆用户信息", produces = "application/json")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = TreatUserInfo.class, message = "响应")
    })
    @RequestMapping(value = "/info")
    public Object loginUserInfo(
    ) {

        return AjaxResult.success(webCustomerService.getTreatUserInfo());
    }

    /**
     * web会诊-找回密码-发送验证码
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "找回密码-发送验证码", notes = "找回密码-发送验证码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/send/recallCode")
    public Object preRecallCode(@RequestParam(value = "phone", required = true) String phone
    ) {
        final Object cacheObject = redisCache.getCacheObject(phone);
        if (!CommonUtils.checkNull(cacheObject)) {
            return AjaxResult.error("验证码尚未过期");
        }
        String token = CommonUtils.getRandomNumber();
        ALiSms.sendSms(phone, "SMS_194026430", "{\"code\":\"" + token + "\"}");
        redisCache.setCacheObject(phone, token, 300, TimeUnit.SECONDS);
        return AjaxResult.success();
    }

    /**
     * web会诊-找回密码-校验手机号
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "找回密码-校验手机号", notes = "找回密码-校验手机号", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/check/phone")
    public Object preCheckPhone(@RequestParam(value = "phone") String phone
    ) {
        final BizHospital bizHospital = new BizHospital();
        bizHospital.setRelationPhone(phone);
        final List<BizHospital> bizHospitals = iBizHospitalService.selectBizHospitalList3(bizHospital);
        if (CommonUtils.checkNull(bizHospitals)) {
            return AjaxResult.error("该手机号未注册");
        }
        return AjaxResult.success();
    }

    /**
     * web会诊-找回密码-校验验证码
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "找回密码-校验验证码", notes = "找回密码-校验验证码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/check/code")
    public Object preCheckCode(@RequestParam(value = "phone") String phone,
                               @RequestParam(value = "code") String code
    ) {
        final Object cacheObject = redisCache.getCacheObject(phone);
        if (CommonUtils.checkNull(cacheObject)) {
            throw new CustomException("验证码过期");
        }
        if (!cacheObject.toString().equals(code)) {
            throw new CustomException("验证码错误");
        }
        return AjaxResult.success();
    }

    /**
     * web会诊-找回密码-重置密码
     *
     * @param phone
     * @return
     */
    @ApiOperation(value = "找回密码-重置密码", notes = "找回密码-重置密码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/reset/pwd")
    public Object preResetPwd(@RequestParam(value = "phone", required = true) String phone,
                              @RequestParam(value = "code", required = true) String code,
                              @RequestParam(value = "password", required = true) String password
    ) {
        final Object cacheObject = redisCache.getCacheObject(phone);
        if (CommonUtils.checkNull(cacheObject)) {
            throw new CustomException("验证码过期");
        }
        if (!cacheObject.toString().equals(code)) {
            throw new CustomException("验证码错误");
        }
        final BizHospital bizHospital = new BizHospital();
        bizHospital.setRelationPhone(phone);
        final List<BizHospital> bizHospitals = iBizHospitalService.selectBizHospitalList3(bizHospital);
        if (CommonUtils.checkNull(bizHospitals)) {
            throw new CustomException("非法手机号码");
        }
        bizHospital.setHospitalID(bizHospitals.get(0).getHospitalID());
        // 开始修改密码
        bizHospital.setPasswd(SecurityUtils.encryptPassword(password));
        redisCache.deleteObject(phone);
        final Integer i = iBizHospitalService.updateBizHospital(bizHospital);
        return AjaxResult.success();
    }
}
